<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>资料修改</title>
		<link rel="stylesheet" href="css/bootstrap.css" />
		<link rel="stylesheet" href="css/publish_article.css" />
		<script src="js/jquery.js"></script>
		<script src="js/bootstrap.js"></script>
		<script src="node_modules/axios/dist/axios.js"></script>
		<script src="js/register.js"></script>
	</head>
	<body>
		<div id="modification">
			<form role="form" id="content" class="col-sm-6">
				<div class="form-group">
					<label for="name">用户邮箱</label>
					<input type="text" disabled class="form-control" id="email" />
				</div>
				<div class="form-group">
					<label for="name">用户手机号</label>
					<input type="text" disabled class="form-control" id="phone" />
				</div>
				<div class="form-group">
					<label for="name">用户昵称</label>
					<input
						type="text"
						class="form-control"
						id="nickname"
						placeholder="输入用户昵称"
					/>
				</div>
				<div class="form-group col-sm-12">
					<div class="col-sm-1"><label>性别：</label></div>
					<div class="col-sm-1">
						<input type="radio" name="gender" class="gender" value="1" />男
					</div>
					<div class="col-sm-1">
						<input type="radio" name="gender" class="gender" value="0" />女
					</div>
				</div>
				<div class="form-group col-sm-12">
					<div class="col-sm-1"><label>权限：</label></div>
					<div class="col-sm-3">
						<input
							type="radio"
							name="authority"
							class="authority"
							value="0"
						/>超级管理员
					</div>
					<div class="col-sm-3">
						<input
							type="radio"
							name="authority"
							class="authority"
							value="1"
						/>普通管理员
					</div>
					<div class="col-sm-3">
						<input type="radio" name="authority" class="authority" value="9" />注册会员
					</div>
				</div>
				<div class="form-group col-sm-12 role_sel">
					<label for="">角色</label>
					<select class="form-control" id="sel"></select>
				</div>
				<input
					type="submit"
					id="sub"
					class="btn btn-info col-sm-offset-6"
					value="修改资料"
				/>
			</form>
		</div>
		<div id="myAlert" class="alert alert-success col-sm-4 col-sm-offset-3">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>文章发布成功!!!</strong>
		</div>
	</body>
	<script>
		let admin = new Admin();
		//判断是否有权限
		admin.jurisdiction();
		let url = new URLSearchParams(location.search.substring(1));
		let userid = admin.getlocationId();
		//判断是否传递了id，没有传递id则从本地取
		if (url.get("id") === null) {
			userid = admin.getLocalStorageId();
		} else {
			userid = url.get("id");
		}
		//创建角色
		admin.createOption("https://ty.php800.cn/api/member/role.php", "#sel", "rolename");
		//获取当前用户信息
		function getnowuser() {
			admin.getone(userid).then((res) => {
				let { authority, roleid, phone, email, nickname, gender } = res[0];
				//设置默认选中
				admin.setVal("#email", email);
				admin.setVal("#nickname", nickname);
				admin.setVal("#phone", phone);
				admin.setchecked("input[name=gender]", gender);
				admin.setchecked("input[name=authority]", authority);
				admin.setchecked("#sel>option", roleid, "selected");
			});
		}
		getnowuser();
		/* =============================================== */
		(function () {
			let formdata = new FormData();
			admin.setformdata(".gender", (val) => {
				formdata.append("gender", val);
			});
			admin.setformdata("input[name=authority]", (val) => {
				formdata.append("authority", val);
			});

			// admin.setformdata("input[name=authority]", "authority", formdata);

			document.querySelector("#content").addEventListener("submit", function (e) {
				e.preventDefault();
				formdata.append("id", userid);
				formdata.append("nickname", document.querySelector("#nickname").value);
				formdata.append("roleid", document.querySelector("#sel").value);

				axios({
					method: "post",
					url: "https://ty.php800.cn/api/member/useredit.php",
					data: formdata,
				}).then((res) => {
					let { verify, msg } = res.data;
					if (verify) {
						getnowuser();
					}
					admin.popUpBox(verify, msg);
				});
			});
		})();
	</script>
</html>
